package com.datastructures.tree;

import java.util.Arrays;

/**
 * @author MaoLin Wang
 * @date 2019/11/520:58
 */
public class ArrayBinaryTreeDemo {
    public static void main(String[] args) {
        int[] arr={1,2,3,4,53,6,7,8,122,144,10};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
      // // ArrayBinaryTree arrayBinaryTree = new ArrayBinaryTree(arr);
        //arrayBinaryTree.preOrder(0);

    }
}

//编写ArrayBianryTree，实现顺序存储二叉树
class ArrayBinaryTree{
    private int[] arr;
    private int size;

    public ArrayBinaryTree(int[] arr){
        this.arr=arr;
        this.size=arr.length;
    }

    public void preOrder(){
        this.preOrder(0);
    }
    /**
     *  完成顺序存储二叉树的遍历
     *  index为数组下标
     */
    public void preOrder(int index){
        if (arr ==null || size==0){
            System.out.println("数组为空");
        }
        System.out.println(arr[index]);
        //向左递归遍历
        if ((index*2+1)<size){
            preOrder(index*2+1);
        }
        //向右递归
        if ((index*2 +2)<size){
            preOrder(2*index+2);
        }
    }
}
